//
//  HomeViewController.swift
//  SwiftProject
//
//  Created by qindong on 2021/3/11.
//

import UIKit

class HomeViewController: BaseViewController, ListAdapterDataSource, UIScrollViewDelegate {
    lazy var adapter : ListAdapter = {
        ListAdapter(updater: ListAdapterUpdater(), viewController: self)
    }()
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    var dataArray : [BaseIGListKitModel] = []

    let topImageV = UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBarType = .clear
    }
    
    override func initSubviews() {
        super.initSubviews()
        
        dataArray = [FQHomeBackgroundModel(), FQHomeImageModel(type: 0), FQHomeImageModel(type: 1), FQHomeImageModel(type: 2)]
        
        collectionView.backgroundColor = .white
        collectionView.contentInsetAdjustmentBehavior = .never
        view.addSubview(collectionView)
        adapter.collectionView = collectionView
        adapter.dataSource = self
        adapter.scrollViewDelegate = self

        collectionView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.bottom.equalTo(-TabBarHeight)
        }
        
        topImageV.backgroundColor = .colorMain
        topImageV.alpha = 0
        view.addSubview(topImageV)
        
        topImageV.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(NavigationContentTop)
        }
        
        if YHBaseConfiguration.shared.configModel == nil {
            YHBaseConfiguration.shared.requestConfig()
        }
    }
    
    func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
        dataArray
    }

    func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
        switch object {
        case is FQHomeImageModel:
            return FQHomeImageController()
        default:
            return FQHomeBackgroundController()
        }
    }

    func emptyView(for listAdapter: ListAdapter) -> UIView? {
        return nil
    }
    
    @objc func buttonClick() {
        
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView.contentOffset.y <= 0 {
            return
        }
        let alpha : CGFloat = scrollView.contentOffset.y / NavigationContentTop
        topImageV.alpha = alpha
    }
    
    override func setupNavigationItems() {
        super.setupNavigationItems()
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: UIImageView(imageString: "icon_baoyinWallet"))
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "icon_home_kefu"), style: .done, target: self, action: #selector(barClick))
    }
    
    @objc func barClick() {
        
    }
    
    override func customNavigationBarTransitionKey() -> String? {
        "home"
    }
}
